package com.demo.key.pa

import android.content.Context
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.util.DisplayMetrics
import androidx.appcompat.app.AppCompatActivity
import com.demo.key.R
import com.gyf.immersionbar.ImmersionBar

abstract class Abs0822Page:AppCompatActivity() {
    var resume0822=false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        density()
        setContentView(layoutId())
        ImmersionBar.with(this).apply {
            fitsSystemWindows(true)
            statusBarColor(R.color.color_23272c)
            init()
        }
        view()
    }

    abstract fun layoutId():Int

    abstract fun view()

    private fun density(){
        val metrics: DisplayMetrics = resources.displayMetrics
        val td = metrics.heightPixels / 760f
        val dpi = (160 * td).toInt()
        metrics.density = td
        metrics.scaledDensity = td
        metrics.densityDpi = dpi
    }

    fun shareApp() {
        val intent = Intent(Intent.ACTION_SEND)
        intent.type = "text/plain"
        intent.putExtra(
            Intent.EXTRA_TEXT,
            "https://play.google.com/store/apps/details?id=${getPackInfo().packageName}"
        )
        startActivity(Intent.createChooser(intent, "share"))
    }


    private fun getPackInfo(): PackageInfo {
        val pm = packageManager
        return pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
    }

    override fun onResume() {
        super.onResume()
        resume0822=true
    }

    override fun onPause() {
        super.onPause()
        resume0822=false
    }

    override fun onStop() {
        super.onStop()
        resume0822=false
    }


    fun updateApp() {
        val packName = getPackInfo().packageName
        val intent = Intent(Intent.ACTION_VIEW).apply {
            data = Uri.parse(
                "https://play.google.com/store/apps/details?id=$packName"
            )
        }
        startActivity(intent)
    }
}